package com.yuantu.judiciary.dao;

import com.yuantu.judiciary.model.domain.InviteCodeDO;
import com.yuantu.judiciary.model.enums.InviteCodeType;
import com.yuantu.judiciary.model.vo.DistributeInfoVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.sql.Timestamp;
import java.util.List;

/**
 * 邀请码
 *
 * @author syw
 * @since 2023-08-18
 **/
public interface IInviteCodeDao extends JpaRepository<InviteCodeDO, Long> {

    InviteCodeDO findCodeByInviteCodeAndTypeAndDeletedFalse(String inviteCode, InviteCodeType type);

    Page<InviteCodeDO> findByOperatorIdInAndTypeAndDeletedFalse(List<Long> operatorId, InviteCodeType type, Pageable pageable);

    @Modifying
    @Query("update InviteCodeDO set deleted = true, modifierId = :accountId, modifyTime = :currentTime " +
            "where ((coalesce (:id) is null and 1=0) or (coalesce (:id) is not null and id in (:id))) " +
            "and deleted = false ")
    void deleteByIdIn(List<Long> id, Long accountId, Timestamp currentTime);

    InviteCodeDO findByIdAndDeletedFalse(Long id);

    List<InviteCodeDO> getOperatorByTypeAndDeletedFalse(InviteCodeType type);

    List<InviteCodeDO> findByOperatorIdAndTypeInAndDeletedFalse(Long operatorId, List<InviteCodeType> type);

    InviteCodeDO findCodeByInviteCodeAndDeletedFalse(String inviteCode);

    List<InviteCodeDO> getInviteCodeByOperatorIdAndTypeAndDeletedFalseOrderByIdDesc(Long operatorId, InviteCodeType type);

    @Query("select id from AccountDO where id not in (select operatorId from InviteCodeDO where type = 'INVITE')")
    List<Long> getAccountListWithoutInviteCode();

    Page<InviteCodeDO> findInfosByInviteCodeAndTypeAndDeletedFalse(String inviteCode, InviteCodeType type, Pageable pageable);

    @Query("select ic from InviteCodeDO ic left join InviteRecordDO ir on ic.inviteCode = ir.inviteCode " +
            "left join AccountDO a on ic.operatorId = a.id " +
            "where ic.type = 'INVITE' and ic.deleted = false and ir.id is not null and ir.deleted = false and a.deleted = false " +
            "and (:distributeCode is null or :distributeCode = '' or ic.inviteCode like concat('%', :distributeCode, '%')) " +
            "group by ic.id ")
    Page<InviteCodeDO> findInfosByInviteCodeAndDeletedFalseForQuery(String distributeCode, Pageable pageable);

    @Query("select ic from InviteCodeDO ic left join AccountDO a on ic.operatorId = a.id left join UserInfoDO u on a.userId = u.id " +
            "where ic.deleted = false and a.deleted = false and u.deleted = false " +
            "and (:accountInfo is null or :accountInfo = '' or (u.userName like concat('%', :accountInfo, '%') or u.phone like concat('%', :accountInfo, '%'))) ")
    Page<InviteCodeDO> findInviteCodeByAccountInfo(String accountInfo, Pageable pageable);

    List<InviteCodeDO> findInfosByOperatorIdInAndTypeAndDeletedFalse(List<Long> accountIds, InviteCodeType type);

    List<InviteCodeDO> findInfosByInviteCodeInAndDeletedFalse(List<String> inviteCodeList);

}
